package com.zheng.http;

import java.io.IOException;
import java.io.InputStream;

/**
 *  @author: zheng
 *  @Date: 2020-06-23 17:38
 *  @Description:
 */
public class Request {

    private String method;//请求方式 get/post

    private String url;

    private String hostName;//localhost:8080

    private InputStream inputStream;//socket 拿出来的输入流

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getHostName() {
        return hostName;
    }

    public void setHostName(String hostName) {
        this.hostName = hostName;
    }

    public InputStream getInputStream() {
        return inputStream;
    }

    public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }

    public Request(InputStream inputStream) throws IOException {
        this.inputStream=inputStream;

        //从输入流里面获取请求信息
        int count=0;
        while (count==0){
            count=inputStream.available();
        }

        byte[] bytes=new byte[count];
        inputStream.read(bytes);

        String inputStr=new String(bytes);

        //获取第一行请求投信息
        String firstLineStr=inputStr.split("\\n")[0]; //Get /Http/1.1

        String secondLineStr=inputStr.split("\\n")[1];//  Host: localhost:8080

        String[] strings = firstLineStr.split(" ");
        String[] strings2 = secondLineStr.split(" ");

        this.method = strings[0];
        this.url = strings[1];
        this.hostName = strings2[1].substring(0, strings2[1].lastIndexOf(":"));

    }
}
